
#include "FarFieldPressure.h"
#include "CFDProblem.h"

template<>
InputParameters validParams<FarFieldPressure>()
{
	InputParameters params = validParams<FarFieldBC>();
	return params;
}

FarFieldPressure::FarFieldPressure(const InputParameters & parameters):
	FarFieldBC(parameters)
{
}

void FarFieldPressure::superSonicInlet()
{
    _cfd_data_neighbor.uh[0] = _rho_inf;
    _cfd_data_neighbor.uh[1] = _rho_inf*_vel_inf(0);
    _cfd_data_neighbor.uh[2] = _rho_inf*_vel_inf(1);
    _cfd_data_neighbor.uh[3] = _rho_inf*_vel_inf(2);
    _cfd_data_neighbor.uh[4] = _p_inf/(_gamma-1) + 0.5*_rho_inf*_vel_inf.norm_sq();
}

void FarFieldPressure::superSonicOutlet()
{
    _cfd_data_neighbor.uh[0] = _cfd_data.uh[0];
    _cfd_data_neighbor.uh[1] = _cfd_data.uh[1];
    _cfd_data_neighbor.uh[2] = _cfd_data.uh[2];
    _cfd_data_neighbor.uh[3] = _cfd_data.uh[3];
    _cfd_data_neighbor.uh[4] = _cfd_data.uh[4];
}

void FarFieldPressure::subSonicInlet()
{
    _cfd_data_neighbor.uh[0] = _rho_inf;
    _cfd_data_neighbor.uh[1] = _rho_inf*_vel_inf(0);
    _cfd_data_neighbor.uh[2] = _rho_inf*_vel_inf(1);
    _cfd_data_neighbor.uh[3] = _rho_inf*_vel_inf(2);
    _cfd_data_neighbor.uh[4] = _cfd_data.p/(_gamma-1) + 0.5*_rho_inf*_vel_inf.norm_sq();
}

void FarFieldPressure::subSonicOutlet()
{
    _cfd_data_neighbor.uh[0] = _cfd_data.uh[0];
    _cfd_data_neighbor.uh[1] = _cfd_data.uh[1];
    _cfd_data_neighbor.uh[2] = _cfd_data.uh[2];
    _cfd_data_neighbor.uh[3] = _cfd_data.uh[3];
    _cfd_data_neighbor.uh[4] = _p_inf/(_gamma - 1) + 0.5*_cfd_data.r*_cfd_data.vel_size;
}

void FarFieldPressure::boundaryCondition()
{
	Real vnl = _cfd_data.vel*_normals[_qp];
	Real vnr = _vel_inf*_normals[_qp];
	if(vnl + vnr <= 0)
	{
		if (_cfd_data.m > 1)
			superSonicInlet();
		else
			subSonicInlet();
	}
	else
	{
		if (_cfd_data.m > 1)
			superSonicOutlet();
		else
			subSonicOutlet();
	}
}
